﻿using System;
using System.IO;
using System.Windows.Forms;
using CSW.Framework.UI.Forms;
using CSW.Framework.UI.Binding;
using LinqBusinessObjectBuilder.Options;
using System.Diagnostics;

namespace LinqBusinessObjectBuilder.Forms
{
    internal partial class GlobalOptionsForm : BaseEditForm
    {
        private readonly EditFormBinder<GlobalOptions> m_Binder;

        public GlobalOptionsForm()
        {
            InitializeComponent();

            m_Binder = new EditFormBinder<GlobalOptions>(this, GlobalOptions.Instance, error, btnSave, null, RecordType.Parent, ShowErrors.Immediately);
            m_Binder.Bind(new BindableTextBox(txtMSBuildPath), "MSBuildPath");
            m_Binder.Bind(new BindableTextBox(txtCommonCSProj), "CSWCommonPath");
            m_Binder.Bind(new BindableTextBox(txtUICSProj), "CSWUIPath");
            m_Binder.Bind(new BindableTextBox(txtUIDevExCSProj), "CSWUIDevExPath");
            m_Binder.UpdateControls();

            if (!TryAssignInitialDirectory(openMSBuildDialog, GlobalOptions.Instance.MSBuildPath))
            {
                openMSBuildDialog.InitialDirectory = Path.Combine(Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.System)), @"Microsoft.NET\Framework\v3.5");
            }

            TryAssignInitialDirectory(openCSWCommonDialog, GlobalOptions.Instance.CSWCommonPath);
            TryAssignInitialDirectory(openCSWUIDialog, GlobalOptions.Instance.CSWUIPath);
            TryAssignInitialDirectory(openCSWUIDevExDialog, GlobalOptions.Instance.CSWUIDevExPath);
        }

        private static bool TryAssignInitialDirectory(FileDialog openFileDialog, string fileName)
        {
            try
            {
                openFileDialog.InitialDirectory = Path.GetDirectoryName(fileName);
                return true;
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex);
                return false;
            }
        }

        private void btnBrowseCSWCommon_Click(object sender, EventArgs e)
        {
            if (openCSWCommonDialog.ShowDialog() == DialogResult.OK)
            {
                GlobalOptions.Instance.CSWCommonPath = openCSWCommonDialog.FileName;
            }
        }

        private void btnBrowseMSBuild_Click(object sender, EventArgs e)
        {
            if (openMSBuildDialog.ShowDialog() == DialogResult.OK)
            {
                GlobalOptions.Instance.MSBuildPath = openMSBuildDialog.FileName;
            }
        }

        private void btnBrowseUICSProj_Click(object sender, EventArgs e)
        {
            if (openCSWUIDialog.ShowDialog() == DialogResult.OK)
            {
                GlobalOptions.Instance.CSWUIPath = openCSWUIDialog.FileName;
            }
        }

        private void btnBrowseUIDevExCSProj_Click(object sender, EventArgs e)
        {
            if (openCSWUIDevExDialog.ShowDialog() == DialogResult.OK)
            {
                GlobalOptions.Instance.CSWUIDevExPath = openCSWUIDevExDialog.FileName;
            }
        }
    }
}
